/**
 * @Classname KqNotifyTimeJob
 * @Description TODO
 * @author Tale-Mi
 * @date 2019/3/11 21:01
 */
package org.tale.mscp.notify.job;

import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.tale.mscp.notify.MscpNotifyApplication;
import org.tale.mscp.notify.entity.BrushCard;
import org.tale.mscp.notify.services.IRecordNotifyService;
import org.tale.mscp.notify.utils.Const;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

public class KqNotifyDayJob extends QuartzJobBean {
    private static final Logger logger = LoggerFactory.getLogger(MscpNotifyApplication.class);
    @Autowired
    public IRecordNotifyService recordNotifyService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${mscp.enable-redis}")
    private boolean enableRedis;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (enableRedis) {
            redisTemplate.opsForValue().append(now, "执行每日考勤通知方法");
        } else {
            logger.info("==========执行每日考勤通知方法============\r\n\r\n");
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDate stdate = LocalDate.parse("2019-01-07",dtf);
//        LocalDate eddate = LocalDate.parse("2019-01-07", dtf);
        LocalDate stdate = LocalDate.now();
        LocalDate eddate = LocalDate.now();
//        List<AttAtdRec> attAtdRecList=recordNotifyService.getKemiRecord(stdate,eddate);
        for (Map.Entry<String, String> entry : Const.USER_INFO_MAP.entrySet()) {
            List<BrushCard> brushCardList = recordNotifyService.getZhongkongRecord(stdate, eddate, entry.getKey(), null);
            recordNotifyService.sendNotifyPerDay(brushCardList, null);
        }
        if (enableRedis) {
            redisTemplate.opsForValue().append(now, "执行完毕");
        } else {
            logger.info("==========执行完毕============\r\n\r\n");
        }
    }
}
